package com.tanhua.dubbo.api;

import com.tanhua.dubbo.api.RecommendUserApi;
import com.tanhua.model.mongo.RecommendUser;
import com.tanhua.model.mongo.UserLike;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class RecommendUserApiImpl implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    //查询评分最高
    @Override
    public RecommendUser finndWithMaxScore(Long toUserId) {
        //根据toUserId查询,根据score评分排序,获取第一条
        //构建 Criteria
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        //构建Query对象
        Query query = new Query(criteria).with(Sort.by(Sort.Order.desc("score"))).limit(1);
        //调用mongoTemplate查询
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        //如果为空,则直接返回结果
        if (recommendUser == null) {
            query = new Query().with(Sort.by(Sort.Order.desc("score"))).limit(1);
            recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        }
        return recommendUser;
    }

    //根据id查询
    @Override
    public List<RecommendUser> findByUserId(Long userId) {
        //构建Query对象
        Query query = new Query(Criteria.where("toUserId").is(userId));
        List<RecommendUser> recommendUserList = mongoTemplate.find(query, RecommendUser.class);
        return recommendUserList;
    }

    //根据用户id和推荐用户id
    @Override
    public RecommendUser findById(Long userId, Long toUserId) {
        Query query = Query.query(Criteria.where("userId").is(userId)
                .and("toUserId").is(toUserId));
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        //如果查询为空 说明两人之间没有缘分值 则需要手动输入一个缘分值
        if (recommendUser == null){
            recommendUser = new RecommendUser();
            recommendUser.setUserId(userId);
            recommendUser.setToUserId(toUserId);
            recommendUser.setScore(90d);
        }
        return recommendUser;
    }


    @Override
    public List<RecommendUser> cards(Long userId, int count) {
        //先获取喜欢 不喜欢 的用户数据
        List<UserLike> userLikeList = mongoTemplate.find(new Query(Criteria.where("userId").is(userId)), UserLike.class);
        List<Long> ids = userLikeList.stream().map(UserLike::getLikeUserId).collect(Collectors.toList());
        //排除查到的用户 再随机推荐count位用户 nin(排除喜欢/不喜欢的用户)
        Criteria criteria = Criteria.where("toUserId").in(ids).and("userId").nin(ids);
        //使用MongoDB的统计api 实现随机查询10个推荐用户
        //创建统计内容
        TypedAggregation<?> aggregation = Aggregation.newAggregation(
                RecommendUser.class,
                Aggregation.match(criteria),//设置查询条件
                Aggregation.sample(10)//设置样本数量需要放在最后
        );
        AggregationResults<RecommendUser> results = mongoTemplate.aggregate(aggregation, RecommendUser.class);

        return results.getMappedResults();
    }
}
